package com.springboot_shopping.service.impl;

import com.springboot_shopping.Mapper.OrderMapper;
import com.springboot_shopping.Mapper.PaymentMapper;
import com.springboot_shopping.entity.Order;
import com.springboot_shopping.entity.Payment;
import com.springboot_shopping.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentMapper paymentMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    @Transactional
    public boolean payOrder(Payment payment) {
        try {
            Date now = new Date();
            payment.setPayTime(now);
            payment.setCreateTime(now);
            payment.setUpdateTime(now);
            payment.setPayStatus(true);

            // 更新订单状态（参数名已统一）
            orderMapper.updateOrderStatus(
                    payment.getOrderId(),  // 对应XML中的 #{id}
                    Order.STATUS_PAID,    // 对应XML中的 #{status}
                    now                   // 对应XML中的 #{payTime}
            );

            return paymentMapper.insert(payment) > 0;
        } catch (Exception e) {
            log.error("支付处理失败: {}", e.getMessage());
            return false;
        }
    }

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PaymentServiceImpl.class);
}